% Run Snow Dots in pure server mode.
% @param duration time in seconds to keep running.  Default is inf.
% @details
% Lets some remote managers run continuously.  Remote managers from
% another Matlab instance (on this or a different machine) may send
% transaction messages which will control behavior here on the server side.
% @details
% Note that for dotsServer to work, dotsTheMessenger must be configured
% with correct IP addresses and ports.  See dotsTheMachineConfiguration for
% how to specify these values for your machines.
%
% @ingroup utilities
function dotsServer(duration)
if ~nargin || isempty(duration)
    duration = inf;
end

% Use this manager for drawing graphics on the server side,
%   or comment out if the this Matlab instance does no drawing.
dotsTheDrawablesManager.reset('serverMode', true, 'clientMode', false);
dm = dotsTheDrawablesManager.theObject;

% Use this manager for playing sounds on the server side,
%   or comment out if the this Matlab instance does no playing.
dotsThePlayablesManager.reset('serverMode', true, 'clientMode', false);
pm = dotsThePlayablesManager.theObject;

% Use this manager for computing results online on the server side,
%   or comment out if the this Matlab instance does no online analysis.
dotsTheComputablesManager.reset('serverMode', true, 'clientMode', false);
cm = dotsTheComputablesManager.theObject;

% Access functions to use for keeping time and yieling CPU cycles.
waitFevalable = dotsTheSwitchboard.getSharedPropertyValue('waitFevalable');
clockFunction = dotsTheSwitchboard.getSharedPropertyValue('clockFunction');

disp(' ')
disp('DOTS SERVER IS RUNNING...')
disp(' ')
endTime = duration + feval(clockFunction);
while endTime > feval(clockFunction);
    % May comment out unused runBriefly() calls.
    dm.runBriefly;
    pm.runBriefly;
    cm.runBriefly;
    feval(waitFevalable{:});
end